﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using MyBind;

public partial class Communicate_BBSSecMangr : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.GridView1.DataKeyNames = new string[] { "id" };
        if (!IsPostBack)
        {
            bind();
        }
    }
    //添加版块
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("BBSSecMangr.aspx");
    }

    //增加属性
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string attriValue = "javascript:return confirm('确定删除吗？')";
                ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onClick", attriValue);
                //绑定版主信息
                string secId = this.GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                string secMst=getSecMst(secId);
                ((Label)e.Row.Cells[3].FindControl("lblSecMst")).Text = secMst;
                //绑定 编辑版主 图标的链接
                string url = "BBSSecAddMst.aspx?secId="+secId;
                ((ImageButton)e.Row.Cells[3].FindControl("imbEditMst")).PostBackUrl=url;
            }
        }
    }
    //修改
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string id = this.GridView1.DataKeys[e.NewEditIndex].Value.ToString();
        Response.Redirect("BBSSecMangr.aspx?id=" + id);
    }
    //删除
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DAL.bbsSection dal = new DAL.bbsSection();
        int id = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
        if (dal.Delete(id) > 0)
        {
            WriteScript.Mesg(this.Response, "删除成功！");
            bind();
        }
    }

    //数据绑定 使用默认源
    protected void bind()
    {
        DAL.bbsSection dal = new DAL.bbsSection(); ;
        DataTable dt = dal.GetList("").Tables[0];
        this.GridView1.DataSource = dt.DefaultView;
        GridView1.DataBind();

        bindDList();
    }
    //重写数据绑定 使用指定源
    protected void bind(DataTable dt)
    {
        this.GridView1.DataSource = dt.DefaultView;
        GridView1.DataBind();
    }

    //下拉列表bind
    protected void bindDList()
    {
        DAL.bbsCatalog cata = new bbsCatalog();
        DataTable dt = cata.GetList("").Tables[0];
        this.DropDownList1.DataSource = dt;
        this.DropDownList1.DataTextField = "name";
        this.DropDownList1.DataValueField = "id";
        this.DropDownList1.DataBind();
        ListItem item = new ListItem();
        item.Text = "--请选择版块类别--";
        //item.Value = "0";
        this.DropDownList1.Items.Insert(0, item);
    }

    //获得版主信息
    protected string getSecMst(string secId)
    {
        string retStr="";
        DAL.bbsSecMaster master=new bbsSecMaster();
        DataTable dt=master.GetDetailedList("secId="+secId).Tables[0];
        if(dt.Rows.Count>0)
        {
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                retStr += dt.Rows[i]["name"].ToString() + ";";
            }
        }else{
            retStr="[暂无]";
        }
        return retStr;
    }

    //选择类别时发生
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.DropDownList1.SelectedIndex > 0)
        {
            string id = this.DropDownList1.SelectedValue;
            DAL.bbsSection dal = new DAL.bbsSection(); ;
            DataTable dt = dal.GetList("cataId=" + id).Tables[0];
            this.bind(dt);
        }
        else
        {
            bind();
        }
    }
}
